<?php

namespace app\admin\controller;

use app\common\model\Practice as PracticeModel;
/**
 * 修行控制器
 * Class Pratice
 * @package app\admin\controller
 */
class Pratice extends Base
{
    /**
     * [getIndex  获取修行设置]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new PracticeModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            $map['type'] = $param['type'];
            $map['status'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        if($param['type'] == 1){
            $name = '念佛';
        }elseif($param['type'] == 2){
            $name = '禅坐';
        }elseif($param['type'] == 3){
            $name = '回向文';
        }
        writelog('查看'.$name.'列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getAdd  添加修行设置]
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new PracticeModel();
        $user = $this->user;
        try{
            $id = $model->addData($param);
        }catch(\Exception $e){
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        if($param['type'] == 1){
            $name = '念佛';
        }elseif($param['type'] == 2){
            $name = '禅坐';
        }elseif($param['type'] == 3){
            $name = '回向文';
        }
        writelog('添加'.$name.'--'.$id,1,$user);
        return admin_json(1, '添加成功');
    }
    /**
     * [getEdit  修改修行设置]
     * @return json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new PracticeModel();
        $user = $this->user;
        try{
            $id = $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        writelog('编辑念佛禅坐回向文--'.$id,1,$user);
        return admin_json(1, '修改成功');
    }
    /**
     * [getDel  删除修行设置]
     * @return json
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $model = new PracticeModel();
        $user = $this->user;
        try{
            $id = $model->editData(['id'=>$param['id'],'status'=>2]);
        }catch(\Exception $e){
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog('删除念佛禅坐回向文--'.$id,1,$user);
        return admin_json(1, '删除成功');
    }
}